package org.example.multiThreading.communication;

public class BandCard {

    private double Money;
    boolean flag = false;
    //true表示可以取钱，false表示不可取钱

    public synchronized void put(double money) throws InterruptedException {
        while (flag){
            //进入等待队列
            this.wait();
        }
        this.Money+=money;
        System.out.println("你爸存了"+money+"元,卡里还剩"+this.Money+"元");
        //改变flag状态
        flag=!flag;
        //唤醒其他线程
        this.notifyAll();
    }

    public synchronized void take(double money) throws InterruptedException {
        //没钱不能取
        while (!flag) {
            this.wait();
        }
        this.Money-=money;
        System.out.println("你取了"+money+"元，卡里还剩"+this.Money+"元。");
        flag=false;
        //唤醒存钱线程
        this.notifyAll();
    }

}
